const express = require("express")
const noteModel = require("../database/model/note")
const verify = require("../middleware/verify")
const noteRouter = express.Router()
// 添加备忘录
noteRouter.post("/note",verify,(req,res)=>{
    let {title,content,username} = req.body
    // res.send({title,content})
    noteModel.create({title,content,username},(err,data)=>{
        if (!err) {
            // console.log(data);
            res.send("添加成功")
        }       
    })
})
// 删除
noteRouter.get("/delNote/:_id",(req,res)=>{
    let{_id} = req.params
    noteModel.deleteOne({_id},(err,data)=>{
        if(err) return
        console.log(data);
    res.send("删除成功")
    })
})
// 修改
noteRouter.get("/updataNote/:_id",(req,res)=>{
    let {_id} = req.params
    noteModel.findById({_id},(err,data)=>{
        console.log(data);
        res.render("updataNote.html",{data})
    })
})
noteRouter.post("/updataNote/:_id",(req,res)=>{
    let {_id} = req.params
    let {title,content} = req.body
    noteModel.updateOne({_id},{title,content},(err,data)=>{
        if (!err) {
            res.send("修改成功")
        }
    })
})


module.exports = noteRouter